import { getSetting } from '../api'
import { setTitle } from '../utils'

export default {
  namespaced: true,
  state: {
    loading: true,
    record: null
  },
  mutations: {
    setLoading (state, payload) {
      state.loading = payload
    },
    setData (state, payload) {
      state.record = payload
    }
  },
  actions: {
    async fetchSetting (context) {
      // 数据拿到了有数据直接返回
      context.commit('setLoading', false)
      if (context.state.record) {
        context.commit('setLoading', true)
        return
      }
      const result = await getSetting()
      context.commit('setLoading', true)
      context.commit('setData', result)
      if (result.siteTitle) {
        setTitle.setSiteTitle(result.siteTitle)
      }
    }
  }
}
